/* Y o u r   D e s c r i p t i o n                       */
/*                            AppBuilder Photon Code Lib */
/*                                         Version 2.01  */

/* Standard headers */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

/* Local headers */
#include "ablibs.h"
#include "global.h"
#include "abimport.h"
#include "proto.h"

extern int arch_opt_ok (PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo);

int
trend_opt_setup( PtWidget_t *link_instance, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
	char **labels;
	double *min_y;
	double *max_y;
	char str [255];
	int i, j;
	PtWidget_t *toggle [5] 	= {ABW_toggle1, ABW_toggle2, ABW_toggle3, ABW_toggle4, ABW_toggle5};
	PtWidget_t *min [5] 	= {ABW_min1, ABW_min2, ABW_min3, ABW_min4, ABW_min5};
	PtWidget_t *max [5] 	= {ABW_max1, ABW_max2, ABW_max3, ABW_max4, ABW_max5};
	unsigned short *mask;
	long *flags;
	time_t *time_win;
	int hours, minutes, seconds;
	int data;

	/* eliminate 'unreferenced' warnings */
	link_instance = link_instance, apinfo = apinfo, cbinfo = cbinfo;

	switch (apinfo->reason) {
		case ABR_PRE_REALIZE:
			PtGetResource (ABW_CwTrend1, TREND_ARG_AXIS_LABELS, &labels, 0);
			PtGetResource (ABW_CwTrend1, TREND_ARG_AXIS_MINY, &min_y, 0);
			PtGetResource (ABW_CwTrend1, TREND_ARG_AXIS_MAXY, &max_y, 0);
			PtGetResource (ABW_CwTrend1, TREND_ARG_TIME_WIN, &time_win, 0);

			for (i=0; i< 5; i++) {
				strcpy (str, labels [i]);
				for (j=0; j< strlen (str); j++) {
					if (str [j] == '\n') str [j] = ',';
				}	
				PtSetResource (toggle [i], Pt_ARG_TEXT_STRING, str, 0);
	
				PtGetResource (ABW_CwTrend1, TREND_ARG_AXIS_MASK, &mask, 0);
				PtGetResource (toggle [i], Pt_ARG_FLAGS, &flags, 0);
				if (((*mask)>> i)&1) *flags |= Pt_SET;
				else *flags &= ~Pt_SET; 
				
				PtSetResource (min [i], Pt_ARG_NUMERIC_VALUE, &min_y [i], 0);
				PtSetResource (max [i], Pt_ARG_NUMERIC_VALUE, &max_y [i], 0);
			}

			hours = *time_win/3600;
			PtSetResource (ABW_razv_hour, Pt_ARG_NUMERIC_VALUE, hours, 0);
			minutes = (*time_win - hours*3600)/60;
			PtSetResource (ABW_razv_min, Pt_ARG_NUMERIC_VALUE, minutes, 0);
			seconds = *time_win - (hours*3600 + minutes*60);
			PtSetResource (ABW_razv_sec, Pt_ARG_NUMERIC_VALUE, seconds, 0);
//			PtRemoveCallback (ABW_opt_ok, Pt_CB_ACTIVATE, (PtCallbackF_t *) arch_opt_ok, NULL );
//			PtAddCallback (ABW_opt_ok, Pt_CB_ACTIVATE, (PtCallbackF_t *) trend_opt_done, NULL);

			data = 1;
			PtSetResource (ABW_opt_ok, Pt_ARG_USER_DATA, &data, sizeof (data));

			break;
	}

	return( Pt_CONTINUE );
}


int
trend_opt_done ( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
	int i;
	double min_y [5];
	double max_y [5];
	PtWidget_t *toggle [5] 	= {ABW_toggle1, ABW_toggle2, ABW_toggle3, ABW_toggle4, ABW_toggle5};
	PtWidget_t *min [5] 	= {ABW_min1, ABW_min2, ABW_min3, ABW_min4, ABW_min5};
	PtWidget_t *max [5] 	= {ABW_max1, ABW_max2, ABW_max3, ABW_max4, ABW_max5};
	unsigned short mask;
	long *flags;
	double *value;
	int *ivalue;
	time_t time_win;
	int *data;

	/* eliminate 'unreferenced' warnings */
	widget = widget, apinfo = apinfo, cbinfo = cbinfo;

	PtGetResource (ABW_opt_ok, Pt_ARG_USER_DATA, &data, 0);
	if ((data) && (*data == 3)) {
		arch_opt_ok (widget, apinfo, cbinfo);
		return( Pt_CONTINUE );
	}

	mask = 0;
	for (i=0; i< 5; i++) {
		PtGetResource (toggle [i], Pt_ARG_FLAGS, &flags, 0);
		if ((*flags)& Pt_SET) mask |= (1<< i);		
		PtGetResource (max [i], Pt_ARG_NUMERIC_VALUE, &value, 0);
		max_y [i] = *value;
		PtGetResource (min [i], Pt_ARG_NUMERIC_VALUE, &value, 0);
		min_y [i] = *value;
	}
	if (mask == 0) mask = 1;

	PtSetResource (ABW_CwTrend1, TREND_ARG_AXIS_MASK, mask, 0);
	PtSetResource (ABW_CwTrend2, TREND_ARG_AXIS_MASK, mask, 0);

	PtSetResource (ABW_CwTrend1, TREND_ARG_AXIS_MINY, min_y, 5);
	PtSetResource (ABW_CwTrend2, TREND_ARG_AXIS_MINY, min_y, 5);
	PtSetResource (ABW_CwTrend1, TREND_ARG_AXIS_MAXY, max_y, 5);
	PtSetResource (ABW_CwTrend2, TREND_ARG_AXIS_MAXY, max_y, 5);

	PtGetResource (ABW_razv_hour, Pt_ARG_NUMERIC_VALUE, &ivalue, 0);
	time_win = (*ivalue)* 3600;
	PtGetResource (ABW_razv_min, Pt_ARG_NUMERIC_VALUE, &ivalue, 0);
	time_win += (*ivalue)* 60;
	PtGetResource (ABW_razv_sec, Pt_ARG_NUMERIC_VALUE, &ivalue, 0);
	time_win += *ivalue;
	PtSetResource (ABW_CwTrend1, TREND_ARG_TIME_WIN, time_win, 0);
	PtSetResource (ABW_CwTrend2, TREND_ARG_TIME_WIN, time_win, 0);

	return( Pt_CONTINUE );
}

int
trend_opt_reset ( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
	PtWidget_t *toggle [5] 	= {ABW_toggle1, ABW_toggle2, ABW_toggle3, ABW_toggle4, ABW_toggle5};
	PtWidget_t *min [5] 	= {ABW_min1, ABW_min2, ABW_min3, ABW_min4, ABW_min5};
	PtWidget_t *max [5] 	= {ABW_max1, ABW_max2, ABW_max3, ABW_max4, ABW_max5};
	double min_def [5]		= {0, 0, 0, 0, 0};
	double max_def [5]		= {4, 120, 900, 250, 250};
	long *flags;
	int i;

	/* eliminate 'unreferenced' warnings */
	widget = widget, apinfo = apinfo, cbinfo = cbinfo;

	for (i=0; i< 5; i++) {
		PtSetResource (min [i], Pt_ARG_NUMERIC_VALUE, &min_def [i], 0);
		PtSetResource (max [i], Pt_ARG_NUMERIC_VALUE, &max_def [i], 0);
		PtGetResource (toggle [i], Pt_ARG_FLAGS, &flags, 0);
		*flags |= Pt_SET;	
		PtDamageWidget (toggle [i]);
	}
	PtSetResource (ABW_razv_hour, Pt_ARG_NUMERIC_VALUE, 0, 0);
	PtSetResource (ABW_razv_min, Pt_ARG_NUMERIC_VALUE, 25, 0);
	PtSetResource (ABW_razv_sec, Pt_ARG_NUMERIC_VALUE, 0, 0);

	return( Pt_CONTINUE );
}

